﻿package mortal.game.view.forging.view.compound
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.greensock.layout.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.common.swfPlayer.*;
    import mortal.common.swfPlayer.data.*;
    import mortal.component.ui.*;
    import mortal.game.cache.*;
    import mortal.game.cache.packCache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.mvc.core.*;

    public class CompoundLeftPart extends GSprite
    {
        private var _onlyUseUnbind:Boolean;
        private var _outPutName:GTextFiled;
        private var _outPutItem:BaseItem;
        private var _consumeBox:GConsumeBox;
        private var _advanceBtn:GButton;
        private var _numericStepper:GNumericStepper;
        private var _lineSprite:GSprite;
        private var _arrowIcon:GImageBitmap;
        private var _recipeArr:Array;
        private var _info:TCompoundConfig;
        private var _arroundEffect:SWFPlayer;
        private var _compoundEffect:SWFPlayer;
        private var _compoundEffect2:SWFPlayer;
        private var _unbindItemList:Array;
        private var _unbindItemAmountList:Array;

        public function CompoundLeftPart()
        {
            this._recipeArr = [];
            this._unbindItemList = [];
            this._unbindItemAmountList = [];
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            var _loc_1:GTextFormat = null;
            super.createDisposedChildrenImpl();
            this._lineSprite = UICompomentPool.getUICompoment(GSprite);
            this._lineSprite.filters = [new GlowFilter(0, 1, 2, 2, 10, 1, false, false)];
            UIFactory.setObjAttri(this._lineSprite, 0, 0, -1, -1, this);
            _loc_1 = GlobalStyle.textFormatBai;
            _loc_1.size = 14;
            this._outPutName = UIFactory.gTextField("", 180, 10, 150, 30, this, GlobalStyle.textFormatBai.center());
            this._outPutItem = UICompomentPool.getUICompoment(BaseItem);
            this._outPutItem.x = 219;
            this._outPutItem.y = 107;
            this._outPutItem.setItemStyle(56, null);
            this.addChild(this._outPutItem);
            this._arroundEffect = UICompomentPool.getUICompoment(SWFPlayer);
            this._arroundEffect.move(this._outPutItem.x + 29, this._outPutItem.y + 24);
            this._arroundEffect.visible = false;
            this.addChild(this._arroundEffect);
            _loc_1.size = 12;
            _loc_1.align = AlignMode.RIGHT;
            _loc_1.bold = false;
            this._numericStepper = UIFactory.gNumericStepper(227, 337, 50, 20, this, 99, 1, "NumericStepper", _loc_1);
            this._numericStepper.configEventListener(Event.CHANGE, this.updateNum);
            this._numericStepper.value = 1;
            this._advanceBtn = UIFactory.gButton(Language.getString(41059), 206, 369, 82, 28, this, "BrownBtn");
            this._advanceBtn.configEventListener(MouseEvent.CLICK, this.compoundItemHandler);
            this._consumeBox = UIFactory.gConsumeBox("", 202 - 25, 404, 0, this);
            this._consumeBox.addItem(ConsumeData.UseMoneyType, EPriceUnit._EPriceUnitGold, 0, Language.getString(41060), "1");
            this._consumeBox.visible = false;
            this._arrowIcon = UIFactory.gImageBitmap(ImagesConst.SynthesisPanelUpIcon, 227, 172, this);
            this._arrowIcon.visible = false;
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.updatePackItem);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.updatePackItem);
            this._arroundEffect.dispose(param1);
            this._arroundEffect = null;
            this._lineSprite.graphics.clear();
            this._lineSprite.dispose(param1);
            this._lineSprite = null;
            this._arrowIcon.dispose(param1);
            this._arrowIcon = null;
            this._advanceBtn.dispose(param1);
            this._advanceBtn = null;
            this._outPutItem.dispose(param1);
            this._outPutItem = null;
            this._outPutName.dispose(param1);
            this._outPutName = null;
            this._numericStepper.dispose(param1);
            this._numericStepper = null;
            this._consumeBox.dispose(param1);
            this._consumeBox = null;
            if (this._compoundEffect)
            {
                this._compoundEffect.dispose(param1);
                this._compoundEffect = null;
            }
            if (this._compoundEffect2)
            {
                this._compoundEffect2.dispose(param1);
                this._compoundEffect2 = null;
            }
            this.removeRecipeArr();
            this._info = null;
            this._unbindItemList.length = 0;
            this._unbindItemAmountList.length = 0;
            super.disposeImpl(param1);
            return;
        }// end function

        private function updatePackItem(param1:Object) : void
        {
            var _loc_2:CompoundRecipe = null;
            for each (_loc_2 in this._recipeArr)
            {
                
                _loc_2.updatePackItem();
            }
            return;
        }// end function

        private function compoundItemHandler(event:MouseEvent) : void
        {
            var dic:Dictionary;
            var getCompoundItem:Function;
            var str:String;
            var a:String;
            var i:int;
            var e:* = event;
            getCompoundItem = function (param1:int) : void
            {
                var _loc_2:CompoundData = null;
                if (param1 == Alert.OK)
                {
                    _loc_2 = new CompoundData();
                    _loc_2.id = _info.id;
                    _loc_2.compoundNum = _numericStepper.value;
                    _loc_2.itemDic = dic;
                    GameProxy.role.getCompoundItem(_loc_2.id, _loc_2.itemDic, _loc_2.compoundNum);
                }
                return;
            }// end function
            ;
            if (this._info == null)
            {
                MsgManager.showRollTipsMsg(Language.getString(41061), MsgRollTipsType.msgRollTips3);
                return;
            }
            if (!Cache.instance.role.enoughMoney(this._info.unit, this._numericStepper.value * this._info.cost))
            {
                if (this._info.unit == EPriceUnit._EPriceUnitCoin)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShowCoinPackWin, this._info.cost));
                }
                else if (this._info.unit == EPriceUnit._EPriceUnitCoinBind)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShowBindCoinPackWin, this._info.cost));
                }
                MsgManager.showRollTipsMsg(GameDefConfig.instance.getEPrictUnitName(this._info.unit) + Language.getString(41062), MsgRollTipsType.msgRollTips3);
                return;
            }
            dic = this.getItemDic();
            if (dic == null)
            {
                MsgManager.showRollTipsMsg(Language.getString(41063), MsgRollTipsType.msgRollTips3);
                return;
            }
            if (false && this._unbindItemList.length > 0)
            {
                str = Language.getString(41064);
                while (i < this._unbindItemList.length)
                {
                    
                    a = i % 2 == 1 ? ("\n") : ("         ");
                    str = str + ((this._unbindItemList[i] as ItemData).itemInfo.htmlName + "*<font color=\'#FAD200\'>" + this._unbindItemAmountList[i] + "</font>" + a);
                    i = (i + 1);
                }
                Alert.show(str, null, Alert.OK | Alert.CANCEL, null, getCompoundItem, null, 4);
                this._unbindItemList.length = 0;
                this._unbindItemAmountList.length = 0;
                return;
            }
            this.getCompoundItem(Alert.OK);
            return;
        }// end function

        private function sortItem(param1:ItemData, param2:ItemData) : int
        {
            if (!ItemsUtil.isPrescriptionItem(param1) && ItemsUtil.isPrescriptionItem(param2))
            {
                return 1;
            }
            if (ItemsUtil.isPrescriptionItem(param1) && !ItemsUtil.isPrescriptionItem(param2))
            {
                return -1;
            }
            if (ItemsUtil.isBind(param1) && !ItemsUtil.isBind(param2))
            {
                return -1;
            }
            if (!ItemsUtil.isBind(param1) && ItemsUtil.isBind(param2))
            {
                return 1;
            }
            return 0;
        }// end function

        private function getItemDic() : Dictionary
        {
            var _loc_3:Array = null;
            var _loc_4:Array = null;
            var _loc_5:int = 0;
            var _loc_6:ItemData = null;
            var _loc_7:int = 0;
            var _loc_8:ItemInfo = null;
            var _loc_10:int = 0;
            var _loc_11:int = 0;
            var _loc_1:* = new Dictionary();
            var _loc_2:* = this._info.recipe.split("&");
            var _loc_9:* = Cache.instance.pack.backPackCache;
            while (_loc_11 < _loc_2.length)
            {
                
                _loc_3 = (_loc_2[_loc_11] as String).split("#");
                _loc_8 = ItemConfig.instance.getConfig(_loc_3[0]);
                _loc_5 = int(_loc_3[1]) * this._numericStepper.value;
                _loc_7 = 0;
                if (!this._onlyUseUnbind)
                {
                    _loc_4 = _loc_9.getItemsByCode(_loc_8.code);
                    if (_loc_4 && _loc_4.length)
                    {
                        _loc_4.sort(this.sortItem);
                        _loc_10 = 0;
                        while (_loc_10 < _loc_4.length)
                        {
                            
                            _loc_6 = _loc_4[_loc_10];
                            _loc_7 = _loc_7 + _loc_6.serverData.itemAmount;
                            if (_loc_7 >= _loc_5)
                            {
                                _loc_1[_loc_6.serverData.uid] = _loc_6.serverData.itemAmount - (_loc_7 - _loc_5);
                                break;
                            }
                            else
                            {
                                _loc_1[_loc_6.serverData.uid] = _loc_6.serverData.itemAmount;
                            }
                            if (!ItemsUtil.isBind(_loc_6))
                            {
                                this._unbindItemList.push(_loc_6);
                                this._unbindItemAmountList.push(_loc_1[_loc_6.serverData.uid]);
                            }
                            _loc_10++;
                        }
                        if (_loc_7 < _loc_5)
                        {
                            return null;
                        }
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    _loc_4 = _loc_9.getItemsByCode(_loc_8.codeUnbind, false);
                    if (_loc_4 && _loc_4.length)
                    {
                        _loc_4.sort(this.sortItem);
                        _loc_10 = 0;
                        while (_loc_10 < _loc_4.length)
                        {
                            
                            _loc_6 = _loc_4[_loc_10];
                            _loc_7 = _loc_7 + _loc_6.serverData.itemAmount;
                            if (_loc_7 >= _loc_5)
                            {
                                _loc_1[_loc_6.serverData.uid] = _loc_6.serverData.itemAmount - (_loc_7 - _loc_5);
                                if (!ItemsUtil.isBind(_loc_6))
                                {
                                    this._unbindItemList.push(_loc_6);
                                    this._unbindItemAmountList.push(_loc_1[_loc_6.serverData.uid]);
                                }
                                break;
                            }
                            else
                            {
                                _loc_1[_loc_6.serverData.uid] = _loc_6.serverData.itemAmount;
                                if (!ItemsUtil.isBind(_loc_6))
                                {
                                    this._unbindItemList.push(_loc_6);
                                    this._unbindItemAmountList.push(_loc_1[_loc_6.serverData.uid]);
                                }
                            }
                            _loc_10++;
                        }
                        if (_loc_7 < _loc_5)
                        {
                            return null;
                        }
                    }
                    else
                    {
                        return null;
                    }
                }
                _loc_11++;
            }
            return _loc_1;
        }// end function

        private function updateNum(event:Event) : void
        {
            this.resetIsUseBindItem(this._onlyUseUnbind);
            this._consumeBox.resetValue("1", this._info.cost * this._numericStepper.value, this._info.unit);
            return;
        }// end function

        private function removeRecipeArr() : void
        {
            var _loc_1:CompoundRecipe = null;
            for each (_loc_1 in this._recipeArr)
            {
                
                _loc_1.dispose();
            }
            this._recipeArr.length = 0;
            return;
        }// end function

        private function createRecipeArr() : void
        {
            var _loc_2:Array = null;
            var _loc_3:CompoundRecipe = null;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:int = 0;
            this._lineSprite.graphics.clear();
            this._lineSprite.graphics.lineStyle(3, 3890822);
            this.removeRecipeArr();
            var _loc_1:* = this._info.recipe.split("&");
            var _loc_4:* = _loc_1.length;
            var _loc_5:int = 80;
            var _loc_6:int = 5;
            while (_loc_10 < _loc_1.length)
            {
                
                _loc_2 = (_loc_1[_loc_10] as String).split("#");
                _loc_3 = UICompomentPool.getUICompoment(CompoundRecipe);
                _loc_3.updateItem(new ItemData(int(_loc_2[0])), _loc_2[1], this._numericStepper.value, this._onlyUseUnbind);
                _loc_9 = int(_loc_10 / _loc_6);
                _loc_7 = _loc_4 - _loc_9 * _loc_6 >= _loc_6 ? (_loc_6) : (_loc_4 - _loc_9 * _loc_6);
                _loc_8 = (506 - _loc_7 * 80 + 15) * 0.5 - 8;
                _loc_3.x = _loc_8 + _loc_5 * (_loc_10 % _loc_6);
                _loc_3.y = 335 - 97 + _loc_9 * _loc_5;
                this.addChild(_loc_3);
                this._recipeArr.push(_loc_3);
                if (_loc_1.length > 1)
                {
                    this._lineSprite.graphics.moveTo(37 + _loc_3.x, _loc_3.y + 4);
                    this._lineSprite.graphics.lineTo(37 + _loc_3.x, _loc_3.y - 20);
                    this._lineSprite.graphics.endFill();
                }
                _loc_10++;
            }
            this._lineSprite.graphics.moveTo(this._recipeArr[0].x + 37, this._recipeArr[0].y - 20);
            this._lineSprite.graphics.lineTo(this._recipeArr[(this._recipeArr.length - 1)].x + 37, this._recipeArr[0].y - 20);
            this._lineSprite.graphics.endFill();
            return;
        }// end function

        public function resetIsUseBindItem(param1:Boolean) : void
        {
            var _loc_2:CompoundRecipe = null;
            var _loc_3:int = 0;
            this._onlyUseUnbind = param1;
            while (_loc_3 < this._recipeArr.length)
            {
                
                _loc_2 = this._recipeArr[_loc_3];
                _loc_2.resetUseBinItem(this._numericStepper.value, this._onlyUseUnbind);
                _loc_3++;
            }
            this._outPutItem.isBind = !this._onlyUseUnbind;
            return;
        }// end function

        public function clearInfo() : void
        {
            this._lineSprite.graphics.clear();
            this.removeRecipeArr();
            this._info = null;
            this._outPutItem.itemData = null;
            this._outPutName.text = "";
            this._consumeBox.visible = false;
            this._arrowIcon.visible = false;
            this._arroundEffect.visible = false;
            this._numericStepper.value = 1;
            return;
        }// end function

        public function updateInfo(param1:TCompoundConfig, param2:Boolean, param3:int) : void
        {
            var _loc_4:* = param1.output.split("#");
            var _loc_5:* = _loc_4[0];
            var _loc_6:* = _loc_4[1];
            this._info = param1;
            this._onlyUseUnbind = param2;
            this.createRecipeArr();
            this._outPutItem.itemData = new ItemData(_loc_5);
            this._outPutItem.amount = _loc_6;
            this._outPutName.htmlText = this._outPutItem.itemData.itemInfo.htmlName;
            this._outPutItem.isBind = !this._onlyUseUnbind;
            if (param1.isBatch)
            {
                param3 = param3 > 0 ? (param3) : (1);
            }
            else
            {
                param3 = 1;
            }
            this._numericStepper.maximum = param3;
            this._numericStepper.value = 1;
            this._consumeBox.visible = true;
            this._consumeBox.resetValue("1", this._info.cost * this._numericStepper.value, this._info.unit);
            this._arrowIcon.visible = true;
            this._arroundEffect.playNum = 0;
            this._arroundEffect.visible = true;
            this._arroundEffect.load("compoundEfect_" + this._outPutItem.itemData.itemInfo.color + ".swf", ModelType.NormalSwf, null);
            return;
        }// end function

        private function playNomalEffect(param1:SWFPlayer) : void
        {
            if (this._compoundEffect)
            {
                this._compoundEffect.dispose();
                this._compoundEffect = null;
            }
            return;
        }// end function

        private function playNomalEffect2(param1:SWFPlayer) : void
        {
            if (this._compoundEffect2)
            {
                this._compoundEffect2.dispose();
                this._compoundEffect2 = null;
            }
            return;
        }// end function

        public function playCompoundSuccessHandler() : void
        {
            if (this._compoundEffect == null)
            {
                this._compoundEffect = UICompomentPool.getUICompoment(SWFPlayer);
                this._compoundEffect.move(this._outPutItem.x + 28, this._outPutItem.y + 30);
                this.addChild(this._compoundEffect);
            }
            this._compoundEffect.playNum = 1;
            this._compoundEffect.load("compoundedEffect.swf", ModelType.NormalSwf, null);
            this._compoundEffect.framesPlayerCompleteHandler = this.playNomalEffect;
            if (this._compoundEffect2 == null)
            {
                this._compoundEffect2 = UICompomentPool.getUICompoment(SWFPlayer);
                this._compoundEffect2.move(this._outPutItem.x + 27, this._outPutItem.y + 27);
                this.addChild(this._compoundEffect2);
            }
            this._compoundEffect2.playNum = 1;
            this._compoundEffect2.load("compoundedEffect_" + this._outPutItem.itemData.itemInfo.color + ".swf", ModelType.NormalSwf, null);
            this._compoundEffect2.framesPlayerCompleteHandler = this.playNomalEffect2;
            return;
        }// end function

    }
}
